package com.atguigu.gmall.weball.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.search.SearchFeignClient;
import com.atguigu.gmall.search.vo.SearchParamVo;
import com.atguigu.gmall.search.vo.SearchResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

/**
 * @author lfy
 * @Description
 * @create 2023-08-26 9:48
 */
@Controller
public class SearchController {

    @Autowired
    SearchFeignClient searchFeignClient;
    /**
     * 检索结果页
     * list.html?category3Id=61&trademark=2:华为&props=23:256G:机身存储&props=24:6寸:屏幕尺寸&props=33:12G:机身内存&order=1:asc&pageNo=2
     * @return
     */
    @GetMapping("/list.html")
    public String searchPage(SearchParamVo param, Model model,
                             HttpServletRequest request){

//        // /以后 ? 之前的叫 URI
//        String requestURI = request.getRequestURI();
//        System.out.println("访问URI："+requestURI);
//
//
//        //完整的是URL
//        StringBuffer requestURL = request.getRequestURL();
//        System.out.println("访问URL："+requestURL.toString());
//
//        String pathInfo = request.getPathInfo();
//        System.out.println("访问PathInfo："+pathInfo);
//
//        String pathTranslated = request.getPathTranslated();
//        System.out.println("访问pathTranslated:"+pathTranslated);
//
//
//        //所有的参数需要拿到再进行拼接
//        Enumeration<String> names = request.getParameterNames();

        //远程调用检索服务
        Result<SearchResultVo> search = searchFeignClient.search(param);
        SearchResultVo data = search.getData();


        //分析页面的数据
        //==== 检索参数
        model.addAttribute("searchParam",data.getSearchParam());


        //==== 面包屑区=====
        //品牌面包屑
        model.addAttribute("trademarkParam",data.getTrademarkParam());
        //属性面包屑
        model.addAttribute("propsParamList",data.getPropsParamList());


        //==== 二次检索区 ===
        //品牌列表
        model.addAttribute("trademarkList",data.getTrademarkList());
        //属性列表
        model.addAttribute("attrsList",data.getAttrsList());


        //==== 排序、分页数据===
        model.addAttribute("orderMap",data.getOrderMap());
        model.addAttribute("pageNo",data.getPageNo());
        model.addAttribute("totalPages",data.getTotalPages());
        model.addAttribute("urlParam",data.getUrlParam());


        //==== 商品数据区
        model.addAttribute("goodsList",data.getGoodsList());




        return "list/index";
    }
}
